package info.openlvb.openface.client.handler;

import info.openlvb.openface.client.base.person.ModifyAccountWindow;
import info.openlvb.openface.client.service.LdapService;
import info.openlvb.openface.client.service.LdapServiceAsync;

import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.gwtext.client.core.EventObject;
import com.gwtext.client.widgets.Button;
import com.gwtext.client.widgets.MessageBox;
import com.gwtext.client.widgets.event.ButtonListenerAdapter;

public class ModifyAccountListener extends ButtonListenerAdapter {

	private String old_pseudo, newlogin, nom, prenom,mail,repperso;
	private ModifyAccountWindow modifyAccountWindow;
	private final LdapServiceAsync ldapService = GWT.create(LdapService.class);
	
	public ModifyAccountListener(ModifyAccountWindow modifyAccountWindow) {
		this.modifyAccountWindow = modifyAccountWindow;
		/*old_pseudo = modifyAccountWindow.getOldPseudo();
		newlogin = modifyAccountWindow.getPseudo().getText();
		nom = modifyAccountWindow.getNom().getText();
		prenom = modifyAccountWindow.getPrenom().getText();
		System.out.println("getmail"+modifyAccountWindow.getMail().getValueAsString());
		mail = modifyAccountWindow.getMail().getText();
		repperso = modifyAccountWindow.getMail().getText();
*/	}

	public void onClick(Button button, EventObject e) {
		//if(modifyAccountWindow.getOldPseudo().equals(modifyAccountWindow.getPseudo().getValueAsString())){
			old_pseudo = modifyAccountWindow.getOldPseudo();
			newlogin = modifyAccountWindow.getPseudo().getText();
			nom = modifyAccountWindow.getNom().getText();
			prenom = modifyAccountWindow.getPrenom().getText();
			mail = modifyAccountWindow.getMail().getText();
			repperso = modifyAccountWindow.getHome().getText();
			
			ldapService.modifyAccount(old_pseudo, newlogin, nom, prenom, mail, repperso, 
						new AsyncCallback<Void>() {
				
				public void onSuccess(Void result) {
					modifyAccountWindow.getListPersonPanel().refresh();
					modifyAccountWindow.hide();
				}
				public void onFailure(Throwable caught) {
					MessageBox.alert("probleme ...", "probleme acces au backend");
				}
			});
			
			
		//}

	}
}
